//
//  InvestmentCell.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/26.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "InvestmentCell.h"
#import "InvestRatioModel.h"

@interface InvestmentCell()

@property (weak, nonatomic) IBOutlet UILabel *investCycleLabel;
@property (weak, nonatomic) IBOutlet UIButton *thirtyDaysLabel;
@property (weak, nonatomic) IBOutlet UIButton *sixtyDaysLabel;
@property (weak, nonatomic) IBOutlet UIButton *oneHEighthLabel;
@property (weak, nonatomic) IBOutlet UILabel *ratioLabel;

@property (nonatomic, strong) NSArray *array;

@property (nonatomic, strong) NSMutableArray *daysBtnArray;

@end

@implementation InvestmentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.investNumLabel.text =  kLocalizedString(@"挖矿数量", nil);
    [self.allNumBtn setTitle:kLocalizedString(@"全部",nil) forState:(UIControlStateNormal)];
    self.investCycleLabel.text = kLocalizedString(@"预期日回报率:", nil);
    
    [self.thirtyDaysLabel setTitle:kLocalizedString(@"", nil) forState:(UIControlStateNormal)];
    [self.sixtyDaysLabel setTitle:kLocalizedString(@"", nil) forState:(UIControlStateNormal)];
    [self.oneHEighthLabel setTitle:kLocalizedString(@"", nil) forState:(UIControlStateNormal)];
}

- (NSMutableArray *)daysBtnArray
{
    if (!_daysBtnArray) {
        _daysBtnArray = [NSMutableArray array];
    }
    return _daysBtnArray;
}

- (IBAction)timeAction:(UIButton *)sender {
    
    if (self.daysBtnClickBlock != nil) {
        self.daysBtnClickBlock(sender.tag,sender.tag == 0 ? @"30" : sender.tag == 1 ? @"60" : @"90");
    }
}

- (void)refreshRatio:(NSString *)ratio {
    self.ratioLabel.text = [NSString stringWithFormat:@"%.2f%%",[ratio floatValue]*100.0];
}

-(void)refreshCellWithButtonTag:(NSInteger)tag withDataArray:(NSArray *)array {
    _array = array;
    
    for (int i = 0; i < self.daysBtnArray.count; i++) {
        UIButton *btn = self.daysBtnArray[i];
        if (btn.tag == tag) {
            if (i > self.daysBtnArray.count - 1) {
                [self refreshNoDataCellWithSelectIndex:i];
            }
            else {
                btn.layer.borderColor   = UIColorFromRGB(0x7889ff).CGColor;
                btn.layer.borderWidth   = 0.5;
                btn.layer.cornerRadius  = 12.5;
                btn.layer.masksToBounds = YES;
                [btn setTitleColor:UIColorFromRGB(0x7889ff) forState:(UIControlStateNormal)];
                self.ratioLabel.text = [NSString stringWithFormat:@"%@%%",[self getRatioWithDays:((InvestRatioItemModel *)_array[i]).investDuration]];
            }
        }
        else {
            btn.layer.borderColor   = [UIColor clearColor].CGColor;
            btn.layer.borderWidth   = 0;
            btn.layer.cornerRadius  = 12.5;
            btn.layer.masksToBounds = NO;
            [btn setTitleColor:UIColorFromRGB(0xc9c9c9) forState:(UIControlStateNormal)];
        }
    }
}

-(void)refreshNoDataCellWithSelectIndex:(NSInteger)index
{
    for (UIButton *btn in self.daysBtnArray) {
        if (index == btn.tag) {
            btn.layer.borderColor   =  UIColorFromRGB(0x7889ff).CGColor;
            btn.layer.borderWidth   = 0.5;
            btn.layer.cornerRadius  = 12.5;
            btn.layer.masksToBounds = YES;
            self.ratioLabel.text = [NSString stringWithFormat:@"0%%"];
            [btn setTitleColor:UIColorFromRGB(0x7889ff) forState:(UIControlStateNormal)];
        }
        else {
            btn.layer.borderColor   =  [UIColor clearColor].CGColor;
            btn.layer.borderWidth   = 0;
            btn.layer.cornerRadius  = 12.5;
            btn.layer.masksToBounds = NO;
            [btn setTitleColor:UIColorFromRGB(0xc9c9c9) forState:(UIControlStateNormal)];
        }
    }
}

-(NSString *)getRatioWithDays:(NSString *)days
{
    for (InvestRatioItemModel *itemModel in self.array) {
        if ([itemModel.investDuration isEqualToString:days]) {
            return itemModel.ratio;
        }
    }
     return @"0";
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
